// api/announcement.js
import request from '@/utils/request'; // 导入你提供的 request 模块
import axios from 'axios';
import { method } from 'lodash'

export const fetchAnnouncements = async (current, size) => {
  try {
    const response = await request({
      url: `/Issue/GetNotice?current=${current}&size=${size}`,
      method: 'post',
    });
    return response;
  } catch (error) {
    console.error('Failed to fetch announcements:', error);
    throw error;
  }
};

export const DownloadAttachments = async (noticeId,filename) => {
  try {
    const response = await request({
      url:`/Issue/DownloadAttachment?TableId=${noticeId}`,
      method: 'post',
      responseType: 'blob', // 设置响应类型为blob
    });

    // 打印完整的响应对象，以便调试
    console.log('Response:', response);

     // 创建一个Blob对象
    const blob = new Blob([response], { type: 'application/zip' });

    // 创建一个临时的URL来表示该Blob对象
    const url = window.URL.createObjectURL(blob);

    // 创建一个<a>元素用于下载
    const a = document.createElement('a');
    a.href = url;
    a.download = filename; // 指定下载的文件名
    document.body.appendChild(a);
    a.click();

    // 下载完成后移除这个<a>元素
    setTimeout(() => {
      document.body.removeChild(a);
      window.URL.revokeObjectURL(url); // 释放这个临时的URL
    }, 0);
  } catch (error) {
    console.error('下载失败:', error);
  }
};

export const fetchAttachments = async (id) => {
    return request({
        url: 'Issue/GetAttachments' + `?TableId=${id}`,
        method: 'post',
    })
};


// 文件上传的API方法
export const uploadFile = async (file, noticeId) => {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('NoticeId', noticeId);

  return request({
    url: 'Issue/SubmitApplication',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
};



export const issueNotice = (formData, config = {}) => {
  return request.post('/Issue/IssueNotice', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    ...config
  });
};

export const deleteAnnouncementsApi = (ids) => {
    return request({
        url: '/Issue/DeleteNotice' + `?TableIds=${ids}`,
        method: 'post'
    })
};


export function postApplications(current, size, value){
    return request({
        url: `Issue/SearchApplication` + '?current=' + current + '&size=' + size,
        method: 'post',
        data: {
            title : value.title,
            studentName : value.studentName,
            status : value.status,
            studentClass : value.studentClass
        }
    })
}

export function exportAttachment(ids){
    return request({
        url: 'Issue/ExportAttachment' + '?ApplicationIds=' + ids,
        method: 'post',
        responseType: 'blob'
    })
}

export function AdminDetermine(fd){
    return request({
        url: 'Issue/Determine',
        method: 'post',
        data: fd,
        headers: {'Content-Type' : 'multipart/form-data'}
    })
}

export function GetUserApplication(current, size, value){
    return request({
        url: 'Issue/GetSelfApplication' + '?current=' + current + '&size=' + size,
        method: 'post',
        data: {
            title : value.title,
            studentName : value.studentName,
            status : value.status,
            studentClass : value.studentClass
        }
    })
}
